From d03bf7c2c420e561d748a308aa6b69ba1a1f65ed Mon Sep 17 00:00:00 2001 From: =?utf8?q?Florian=20M=C3=BCllner?= Date: Sun, 13 Apr 2025 03:07:16 +0200 Subject: [PATCH] ci: Use python to extract JS snippets from style guide The job that lints code snippets in the style guide only runs when either the documentation or the linting rules are changed. Because of that it went unnoticed that the awk tool it uses to extract the snippets is no longer available since the CI image was rebased to F42. Given that awk is awkward anyway, use the opportunity to rewrite the snippet extraction in python instead of adding awk back to the image. Part-of: --- .gitlab-ci/eslint-coding-style.sh | 60 +++++++++++++++---------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/.gitlab-ci/eslint-coding-style.sh b/.gitlab-ci/eslint-coding-style.sh index 772d7463e..39f00530e 100755 --- a/.gitlab-ci/eslint-coding-style.sh +++ b/.gitlab-ci/eslint-coding-style.sh @@ -6,36 +6,36 @@ trap "rm -rf $OUTDIR" EXIT # Turn ```javascript``` code snippets in the # style guide into .js files in $OUTDIR -awk --assign dir=$OUTDIR -- ' - BEGIN { - do_print = 0; - cur = 0; - } - - # end of code snippet - /```$/ { - do_print = 0; - cur++; - } - - do_print { - # remove one level of indent - sub(" ","") - - # the following are class snippets, turn them - # into functions to not confuse eslint - sub("moveActor","function moveActor") - sub("desaturateActor","function desaturateActor") - - # finally, append to the currently generated .js file - print >> dir "/" cur ".js"; - } - - # start of code snippet - /```javascript$/ { - do_print = 1; - } -' docs/js-coding-style.md +cat <<'EOF' | python3 - docs/js-coding-style.md $OUTDIR +import sys +import re + +def extract_js_snippets(input_file, output_dir): + with open(input_file, 'r') as file: + content = file.read() + + # Find all JavaScript code blocks using regex + js_blocks = re.findall(r'```javascript\n(.*?)\n?```', content, flags=re.DOTALL) + + for i, (match) in enumerate(js_blocks): + js_code = match + + # Remove one level of indent + js_code = re.sub(r'^ {4}', '', js_code, flags=re.MULTILINE) + + # The following are class snippets, turn them + # into functions to not confuse eslint + js_code = re.sub(r'^moveActor', 'function moveActor', js_code) + js_code = re.sub(r'^desaturateActor', 'function desaturateActor', js_code) + + # Finally, create a .js file in the output directory + output_filename = f'{output_dir}/{i}.js' + with open(output_filename, 'w') as out_file: + out_file.write(f'{js_code}\n') + +input_file, output_dir = sys.argv[1:] +extract_js_snippets(input_file, output_dir) +EOF eslint \ --rule 'no-undef: off' \ -- 2.39.5